package cn.kgc.liverservice.service;


import cn.kgc.bean.*;
import cn.kgc.myhousemapper.mapper.*;
import cn.kgc.service.AliyunSms;
import cn.kgc.service.LiverService;
import cn.kgc.service.MailService;
import cn.kgc.utils.CrowdFundingUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.System;
import java.util.*;

@Service
@Component
public class LiverServiceImpl implements LiverService {
    @Resource
    LiverMapper liverMapper;
    @Resource
    LiverorderMapper liverorderMapper;
    @Resource
    WxfkMapper wxfkMapper;
    @Resource
    HouseMapper houseMapper;
    @Resource
    ManageMapper manageMapper;
    @Autowired
    MailService mailService;
    @Resource
    RoomMapper roomMapper;

    @Override
    public List<Liver> selectLiverList() {
        List<Liver> livers = liverMapper.selectList(null);
        return livers;
    }

    @Override
    public Liver login(String loginName, String password) {
/*        LiverExample liverExample = new LiverExample();
        liverExample.or().andEmailEqualTo(loginName);
        liverExample.or().andPhoneEqualTo(loginName);*/
        List<Liver> livers = liverMapper.selectList(new QueryWrapper<Liver>().eq("email",loginName).or().eq("phone",loginName));
        if (livers.size() <= 0) {
            return null;
        } else {
            if (CrowdFundingUtils.md5(password).equals(livers.get(0).getPassword())) {
                if (livers.get(0).getState() <= 0) {
                    return null;
                } else {
                    return livers.get(0);
                }

            } else {
                Liver liver = new Liver();
                liver.setId(0);
                return liver;
            }
        }
    }

    @Override
    public int regist(String phone, String email, String password, String code) {
/*        LiverExample liverExample = new LiverExample();
        LiverExample.Criteria criteria = liverExample.createCriteria();
        criteria.andEmailEqualTo(email);
        criteria.andPhoneEqualTo(phone);*/
        List<Liver> livers = liverMapper.selectList(new QueryWrapper<Liver>().eq("email",email).eq("phone",phone));
        if (livers.size()<=0){
            System.out.println("null");
            return 0;
        }else{
            System.out.println("不为null");
            Liver liver=livers.get(0);
            if (Objects.equals(CrowdFundingUtils.md5(code),liver.getCode())) {
                System.out.println("执行正确,开始判断是否设置密码");
                if (liver.getPassword()!=null){
                    return 1;
                }
                liver.setPassword(CrowdFundingUtils.md5(password));
                liver.setMoney(0.0);
                liver.setYanjin(0.0);
                liver.setEndtime(new Date());
                liver.setStarttime(new Date());
                liver.setState(1);
                int i = liverMapper.updateById(liver);
                if (i>0){
                    return 2;
                }else{
                    return 0;
                }

            }else{
                System.out.println("Code不匹配");
                System.out.println("接收到的code"+code);
                System.out.println("加密后:"+CrowdFundingUtils.md5(code));
                System.out.println("数据库:"+liver.getCode());
                return 0;
            }
        }

    }


    @Override
    public int update(Liver liver) {
        return liverMapper.updateById(liver);
    }

    @Override
    public Liver getLiverById(Integer id) {
        return liverMapper.selectById(id);
    }

    @Override
    public PageInfo<Liverorder> selectOrders(Integer pageNum, Integer pageSize, Integer id, Date startTime, Date endTime) {
        PageHelper.startPage(pageNum,pageSize);
/*        LiverorderExample liverorderExample=new LiverorderExample();
        LiverorderExample.Criteria criteria = liverorderExample.createCriteria();*/
        QueryWrapper<Liverorder> queryWrapper=new QueryWrapper<>();
        if (startTime!=null&&!startTime.toString().equals("Mon Jun 06 00:00:00 CDT 1988")&&endTime!=null&&!endTime.toString().equals("Mon Jun 06 00:00:00 CDT 1988")){
            queryWrapper.between("date",startTime,endTime);
        }
        queryWrapper.eq("uid",id);
        List<Liverorder> liverorders = liverorderMapper.selectList(queryWrapper);
        PageInfo<Liverorder> pageInfo=new PageInfo<>(liverorders);
        return pageInfo;
    }

    @Override
    public int addWxfk(Wxfk wxfk) {
        return wxfkMapper.insert(wxfk);
    }

    @Override
    public Wxfk getWxfkById(Integer id) {
        return wxfkMapper.selectById(id);
    }

    @Override
    public House getHouseById(Integer houseId) {
        return houseMapper.selectById(houseId);
    }


    @Override
    public Map<String,Object> addLiverCode(String code, String registName, Integer type) {
        Map<String,Object> map=new HashMap<>();
        List<Liver> livers=null;
        String phone="";
        String email="";
        String codemd5= CrowdFundingUtils.md5(code);
        if (type==null){
            if (registName.contains("@")){
                email=registName;
                mailService.sendSimpleMail(email,"MyHouse公寓管家","您的验证码为:"+code);
               /* LiverExample liverExample=new LiverExample();
                LiverExample.Criteria criteria = liverExample.createCriteria();
                criteria.andEmailEqualTo(email);*/
                livers= liverMapper.selectList(new QueryWrapper<Liver>().eq("email",email));
            }else{
                phone=registName;
                AliyunSms.sendSms(phone,code);
/*                LiverExample liverExample=new LiverExample();
                LiverExample.Criteria criteria = liverExample.createCriteria();
                criteria.andPhoneEqualTo(phone);*/
                livers= liverMapper.selectList(new QueryWrapper<Liver>().eq("phone",phone));
            }
        }else{


        if (type==0){
            if (registName.contains("@")){
                map.put("result","error");
                return map;
            }
            phone=registName;
            AliyunSms.sendSms(phone,code);
/*            LiverExample liverExample=new LiverExample();
            LiverExample.Criteria criteria = liverExample.createCriteria();
            criteria.andPhoneEqualTo(phone);*/
            livers=liverMapper.selectList(new QueryWrapper<Liver>().eq("phone",phone));
        }else if (type==1){
            if (!registName.contains("@")){
                map.put("result","error");
                return map;
            }
            email=registName;
            mailService.sendSimpleMail(email,"MyHouse公寓管家","您的验证码为:"+code);
/*            LiverExample liverExample=new LiverExample();
            LiverExample.Criteria criteria = liverExample.createCriteria();
            criteria.andEmailEqualTo(email);*/
            livers= liverMapper.selectList(new QueryWrapper<Liver>().eq("email",email));
        }
        }
        if (livers.size()>0){
            Liver liver=livers.get(0);
            liver.setCode(codemd5);
            int i = liverMapper.updateById(liver);
            if (i>0){
                map.put("result","success");
                return map;
            }else{
                map.put("result","error");
                return map;
            }
        }else{
            Liver liver=new Liver();
            liver.setCode(codemd5);
            liver.setPhone(phone);
            liver.setEmail(email);
            int insert = liverMapper.insert(liver);
            if (insert>0){
                map.put("result","success");
                return map;
            }else{
                map.put("result","error");
                return map;
            }
        }



    }

    @Override
    public int addLiverOrder(Liverorder liverorder) {
        return liverorderMapper.insert(liverorder);
    }

    @Override
    public Liverorder selectOrderByOrderNo(String orderno) {
/*        LiverorderExample liverorderExample=new LiverorderExample();
        LiverorderExample.Criteria criteria = liverorderExample.createCriteria();*/
/*        criteria.andOrdernoEqualTo(orderno);*/
        List<Liverorder> liverorders = liverorderMapper.selectList(new QueryWrapper<Liverorder>().eq("orderno",orderno));

        return liverorders.get(0);
    }

    @Override
    public int updateLiverOrderState(Liverorder liverorder) {
        return liverorderMapper.updateById(liverorder);
    }

    @Override
    public Manage selectZmanageByHouseId(Integer id) {
/*        ManageExample manageExample=new ManageExample();
        ManageExample.Criteria criteria = manageExample.createCriteria();
        criteria.andHouseidEqualTo(id);
        criteria.andIszEqualTo(1);*/
        List<Manage> manages = manageMapper.selectList(new QueryWrapper<Manage>().eq("houseid",id).eq("isz",1));;
        if (manages.size()>0){
            return manages.get(0);
        }else{
            return null;
        }
    }

    @Override
    public void updateZmanagerMoney(Manage zmanager) {
        manageMapper.updateById(zmanager);
    }

    @Override
    public Room selectRoomById(Integer id) {
        Room room = roomMapper.selectById(id);
        return room;
    }

    @Override
    public List<Liver> selectLiverByRoomId(Integer id) {
/*        LiverExample liverExample=new LiverExample();
        LiverExample.Criteria criteria = liverExample.createCriteria();
        criteria.andRoomidEqualTo(id);*/
        List<Liver> livers = liverMapper.selectList(new QueryWrapper<Liver>().eq("roomid",id));
        return livers;
    }

    @Override
    public PageInfo<Liver> getLiver(Integer pageNum,Integer pageSize,String loginName) {
        PageHelper.startPage(pageNum,pageSize);
/*        LiverExample liverExample=new LiverExample();
        LiverExample.Criteria criteria = liverExample.createCriteria();*/
        QueryWrapper<Liver> queryWrapper=new QueryWrapper<>();
        if (loginName!=null&&!loginName.equals("")){
            queryWrapper.eq("email",loginName).or().eq("phone",loginName);
/*           liverExample.or().andEmailEqualTo(loginName);
           liverExample.or().andPhoneEqualTo(loginName);*/
        }
        List<Liver> livers = liverMapper.selectList(queryWrapper);
        PageInfo<Liver> pageInfo=new PageInfo<>(livers);
        return pageInfo;
    }
}
